package org.github.core.util;

import lombok.val;
import static cn.hutool.core.bean.BeanUtil.edit;
import static cn.hutool.core.text.CharSequenceUtil.isBlank;
import static cn.hutool.core.util.ArrayUtil.containsIgnoreCase;
import static cn.hutool.core.util.ReflectUtil.getFieldValue;
import static cn.hutool.core.util.ReflectUtil.setFieldValue;

public interface TrimUtil {
  static <T> T trimObj(T bean, String... ignoreFields) {
    return edit(bean, field -> {
      if (ignoreFields != null && containsIgnoreCase(ignoreFields, field.getName())) return field;
      if (!String.class.equals(field.getType())) return field;
      val str = (String) getFieldValue(bean, field);
      val val = trimStr(str);
      setFieldValue(bean, field, val);
      return field;
    });
  }

  static String trimStr(String str) {
    return isBlank(str) ? null : str.trim();
  }
}
